package com.cwh.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
//异常处理
public class EmployeeExceptionResolver implements HandlerExceptionResolver {
	
	//将异常处理，转发到错误视图
	@Override
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		
		ex.printStackTrace();
		
		String message=null;
		
		EmployeeException loginException =null;
		
		if(ex instanceof EmployeeException) {
			loginException =(EmployeeException)ex;
			
		}else {
			//针对非CustomException异常，对这类重新构造成一个CustomException，异常信息为“未知错误”
			loginException = new EmployeeException("未知错误");
		}
		//错误信息
		message=loginException.getMessage();
		//将错误信息存储起来
//		request.setAttribute("message", message);
		ModelAndView mv=new ModelAndView();
		mv.addObject("message", message);
		mv.setViewName("forward:/error");
		return mv;
	}
	

}
